var PointReword = {//会员体系兑换商品管理
    id: "ProductNameTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

PointReword.initColumn = function () {// 初始化表格的列
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '商品名称', field: 'productName', align: 'center', valign: 'middle', sortable: true},
        {title: '商品价格', field: 'productPrice', align: 'center', valign: 'middle', sortable: true},
        {title: '市场价格', field: 'marketPrice', align: 'center', valign: 'middle', sortable: true},
        {title: '上架数量', field: 'putawayNumber', align: 'center', valign: 'middle', sortable: true},
        {title: '商品描述', field: 'productDescription', align: 'center', valign: 'middle', sortable: true},
        {title: '上架时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'number', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
        {title: '区域', field: 'areas', align: 'center', valign: 'middle', sortable: true}
    ];
};

PointReword.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PointReword.seItem = selected[0];
        return true;
    }
};

PointReword.addPointReword = function () {//点击添加商品
    var id = "1";
    $.ajax({
        url: Feng.ctxPath + "/pointReword/pointReword_getAdd/" + id,
        type: "post",
        data: {"id": id},
        success: function (data) {
            var index = layer.open({
                type: 2,
                title: '添加商品',
                area: ['60%', '70%'], //宽高
                fix: true, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/pointReword/pointReword_add/' + id
            });
            this.layerIndex = index;
        },
        error: function (data) {
            alert('权限被限制，请联系管理员!');
        }
    })
};

PointReword.updatePointReword = function () {//修改商品
    if (this.check()) {
        var id = PointReword.seItem.id;
        $.ajax({
            url: Feng.ctxPath + "/pointReword/pointReword_getUpdate/" + id,
            type: "post",
            data: {"id": id},
            success: function (data) {
                if (data == 2) {
                    alert("该商品没有图片详情展示，不能修改。如需修改，请删除该商品后重新上传...")
                } else {
                    var index = layer.open({
                        type: 2,
                        title: '修改商品',
                        area: ['60%', '70%'], //宽高
                        fix: true, //不固定
                        maxmin: true,
                        content: Feng.ctxPath + '/pointReword/pointReword_update/' + PointReword.seItem.id
                    });
                    this.layerIndex = index;
                }
            },
            error: function (data) {
                alert('权限被限制，请联系管理员!');
            }
        })
    }
};

PointReword.detailsPointReword = function () {//编辑详情
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改商品',
            area: ['60%', '70%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/pointReword/pointReword_details/' + PointReword.seItem.id
        });
        this.layerIndex = index;
    }
};

PointReword.delete = function () {//删除商品
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/pointReword/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            window.PointReword.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

PointReword.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['productName'] = $("#productName").val();
    queryData['area'] = $("#area").val();
    return queryData;
};

PointReword.resetSearch = function () {
    $("#productName").val("");
    $("#area").val("");
    PointReword.search();
};

PointReword.search = function () {//查询列表
    PointReword.table.refresh({query: PointReword.formParams()});
};

$(function () {
    var defaultColunms = PointReword.initColumn();
    var table = new BSTable(PointReword.id, "/pointReword/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(PointReword.formParams());
    PointReword.table = table.init();
    PointReword.table = table;
});